package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Asignaturaxexamen;
import com.lostreseditores.sabersai.model.dto.AsignaturaxexamenDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class AsignaturaxexamenView {
    private InputText txtAsigxexamordn;
    private InputText txtAsigxexampregn;
    private InputText txtAsigidn_Asignatura;
    private InputText txtExamidn_Examen;
    private InputText txtAsigxexamidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<AsignaturaxexamenDTO> data;
    private AsignaturaxexamenDTO selectedAsignaturaxexamen;

    public AsignaturaxexamenView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            AsignaturaxexamenDTO asignaturaxexamenDTO = (AsignaturaxexamenDTO) e.getObject();

            if (txtAsigxexamordn == null) {
                txtAsigxexamordn = new InputText();
            }

            txtAsigxexamordn.setValue(asignaturaxexamenDTO.getAsigxexamordn());

            if (txtAsigxexampregn == null) {
                txtAsigxexampregn = new InputText();
            }

            txtAsigxexampregn.setValue(asignaturaxexamenDTO.getAsigxexampregn());

            if (txtAsigidn_Asignatura == null) {
                txtAsigidn_Asignatura = new InputText();
            }

            txtAsigidn_Asignatura.setValue(asignaturaxexamenDTO.getAsigidn_Asignatura());

            if (txtExamidn_Examen == null) {
                txtExamidn_Examen = new InputText();
            }

            txtExamidn_Examen.setValue(asignaturaxexamenDTO.getExamidn_Examen());

            if (txtAsigxexamidn == null) {
                txtAsigxexamidn = new InputText();
            }

            txtAsigxexamidn.setValue(asignaturaxexamenDTO.getAsigxexamidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtAsigxexamordn != null) {
            txtAsigxexamordn.setValue(null);
            txtAsigxexamordn.setDisabled(true);
        }

        if (txtAsigxexampregn != null) {
            txtAsigxexampregn.setValue(null);
            txtAsigxexampregn.setDisabled(true);
        }

        if (txtAsigidn_Asignatura != null) {
            txtAsigidn_Asignatura.setValue(null);
            txtAsigidn_Asignatura.setDisabled(true);
        }

        if (txtExamidn_Examen != null) {
            txtExamidn_Examen.setValue(null);
            txtExamidn_Examen.setDisabled(true);
        }

        if (txtAsigxexamidn != null) {
            txtAsigxexamidn.setValue(null);
            txtAsigxexamidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Asignaturaxexamen entity = null;

        try {
            Integer asigxexamidn = new Integer(txtAsigxexamidn.getValue()
                                                              .toString());
            entity = BusinessDelegatorView.getAsignaturaxexamen(asigxexamidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtAsigxexamordn.setDisabled(false);
            txtAsigxexampregn.setDisabled(false);
            txtAsigidn_Asignatura.setDisabled(false);
            txtExamidn_Examen.setDisabled(false);
            txtAsigxexamidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtAsigxexamordn.setValue(entity.getAsigxexamordn());
            txtAsigxexamordn.setDisabled(false);
            txtAsigxexampregn.setValue(entity.getAsigxexampregn());
            txtAsigxexampregn.setDisabled(false);
            txtAsigidn_Asignatura.setValue(entity.getAsignatura().getAsigidn());
            txtAsigidn_Asignatura.setDisabled(false);
            txtExamidn_Examen.setValue(entity.getExamen().getExamidn());
            txtExamidn_Examen.setDisabled(false);
            txtAsigxexamidn.setValue(entity.getAsigxexamidn());
            txtAsigxexamidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveAsignaturaxexamen(FacesUtils.checkInteger(
                    txtAsigxexamidn),
                FacesUtils.checkInteger(txtAsigxexamordn),
                FacesUtils.checkInteger(txtAsigxexampregn),
                FacesUtils.checkInteger(txtAsigidn_Asignatura),
                FacesUtils.checkInteger(txtExamidn_Examen));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteAsignaturaxexamen(FacesUtils.checkInteger(
                    txtAsigxexamidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateAsignaturaxexamen(FacesUtils.checkInteger(
                    txtAsigxexamidn),
                FacesUtils.checkInteger(txtAsigxexamordn),
                FacesUtils.checkInteger(txtAsigxexampregn),
                FacesUtils.checkInteger(txtAsigidn_Asignatura),
                FacesUtils.checkInteger(txtExamidn_Examen));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtAsigxexamidn == null) {
                txtAsigxexamidn = new InputText();
            }

            txtAsigxexamidn.setValue(selectedAsignaturaxexamen.getAsigxexamidn());

            BusinessDelegatorView.deleteAsignaturaxexamen(FacesUtils.checkInteger(
                    txtAsigxexamidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedAsignaturaxexamen);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer asigxexamidn,
        Integer asigxexamordn, Integer asigxexampregn,
        Integer asigidn_Asignatura, Integer examidn_Examen)
        throws Exception {
        try {
            BusinessDelegatorView.updateAsignaturaxexamen(asigxexamidn,
                asigxexamordn, asigxexampregn, asigidn_Asignatura,
                examidn_Examen);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("AsignaturaxexamenView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtAsigxexamordn() {
        return txtAsigxexamordn;
    }

    public void setTxtAsigxexamordn(InputText txtAsigxexamordn) {
        this.txtAsigxexamordn = txtAsigxexamordn;
    }

    public InputText getTxtAsigxexampregn() {
        return txtAsigxexampregn;
    }

    public void setTxtAsigxexampregn(InputText txtAsigxexampregn) {
        this.txtAsigxexampregn = txtAsigxexampregn;
    }

    public InputText getTxtAsigidn_Asignatura() {
        return txtAsigidn_Asignatura;
    }

    public void setTxtAsigidn_Asignatura(InputText txtAsigidn_Asignatura) {
        this.txtAsigidn_Asignatura = txtAsigidn_Asignatura;
    }

    public InputText getTxtExamidn_Examen() {
        return txtExamidn_Examen;
    }

    public void setTxtExamidn_Examen(InputText txtExamidn_Examen) {
        this.txtExamidn_Examen = txtExamidn_Examen;
    }

    public InputText getTxtAsigxexamidn() {
        return txtAsigxexamidn;
    }

    public void setTxtAsigxexamidn(InputText txtAsigxexamidn) {
        this.txtAsigxexamidn = txtAsigxexamidn;
    }

    public List<AsignaturaxexamenDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataAsignaturaxexamen();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<AsignaturaxexamenDTO> asignaturaxexamenDTO) {
        this.data = asignaturaxexamenDTO;
    }

    public AsignaturaxexamenDTO getSelectedAsignaturaxexamen() {
        return selectedAsignaturaxexamen;
    }

    public void setSelectedAsignaturaxexamen(
        AsignaturaxexamenDTO asignaturaxexamen) {
        this.selectedAsignaturaxexamen = asignaturaxexamen;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
